<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    protected $table = 'users';
    protected $fillable = ['user_name', 'password','nick_name','id'];
    protected $hidden = ['password'];
    public $incrementing = false;

    public function token()
    {
        return $this->hasOne('App\\Model\\Tokens','user_id','id');
    }

    public function friends()
    {
        return $this->belongsToMany('App\\Model\\Users','friends','user_id','friend_id');
    }

    public function group()
    {
        return $this->belongsToMany('App\\Model\\Group','user_group','user_id','group_id');
    }

    public function reg($data)
    {
        try{

            $id = create_user_id();
            $data['id'] = $id;
            $data['nick_name'] = $data['user_name'];
            Users::create($data);
            $token = create_token();
            Tokens::create(['token'=>$token,'user_id'=>$id]);

            return ['id'=>$id,'token'=>$token];

        } catch (\Exception $exception)
        {
            return false;
        }

    }

    public function login()
    {

    }
}
